package com.scs.application.config;

import com.scs.application.consts.ErrorCodes;
import com.scs.application.core.model.CommonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

/**
 * @Description:
 */
@Slf4j
@Configuration
@RestControllerAdvice
public class ExtExceptionHandleConfig {

    @ConditionalOnClass(BadSqlGrammarException.class)
    @ExceptionHandler(value = {BadSqlGrammarException.class})
    public ResponseEntity<CommonResult> badSqlGrammarExceptionHandle(BadSqlGrammarException ex, HttpServletRequest request) {
        log.error("sql parse error , the sql is " + ex.getSql(), ex);
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(CommonResult.error(ErrorCodes.INTERNAL_SERVER_ERROR, "SQL解析失败，详细信息请查看后台日志", null));
    }
}
